<template>
  <el-row :gutter="4">
    <el-col style="width: 290px">
      <el-form-item>
        <el-radio-group v-if="options" size="mini" v-model="dateRangeType" @change="setDateRange">
          <el-radio-button label="今日">今日</el-radio-button>
          <el-radio-button label="本周">本周</el-radio-button>
          <el-radio-button label="本月">本月</el-radio-button>
          <el-radio-button label="上月">上月</el-radio-button>
          <el-radio-button label="全部">全部</el-radio-button>
        </el-radio-group>
      </el-form-item>
    </el-col>
    <el-col :span="3">
      <el-form-item>
        <el-date-picker size="mini" v-model="query.beginSearchTime" placeholder="开始时间" type="datetime" value-format="timestamp" format="yyyy-MM-dd HH:mm" clearable/>
      </el-form-item>
    </el-col>
    <el-col :span="3">
      <el-form-item>
        <el-date-picker size="mini" v-model="query.endSearchTime" placeholder="结束时间" type="datetime" value-format="timestamp" format="yyyy-MM-dd HH:mm" clearable/>
      </el-form-item>
    </el-col>
    <slot></slot>
    <el-col :span="3">
      <el-button size="mini" type="primary" plain @click="search">查询</el-button>
    </el-col>
  </el-row>
</template>

<script>
import dateRange from '@/utils/dateRange'

export default {
  props: {
    options: {
      default: true
    }
  },
  data() {
    return {
      dateRangeType: '今日',
      query: {
        beginSearchTime: '',
        endSearchTime: ''
      }
    }
  },
  watch: {
    '$attrs.value': {
      immediate: true,
      deep: true,
      handler(query) {
        if (query) {
          this.query = query
        }
      }
    },
    query: {
      deep: true,
      handler() {
        if (!this.query.beginSearchTime && !this.query.endSearchTime) {
          this.dateRangeType = '全部'
        } else if (!this.query.beginSearchTime || !this.query.endSearchTime) {
          this.dateRangeType = ''
        }
      }
    }
  },
  // created() {
  //   console.log(this.$attrs.value)
  // },
  // watch: {
  //   '$attrs.value': {
  //     immediate: true,
  //     deep: true,
  //     handler(searchQuery) {
  //       // console.log(searchQuery)
  //       searchQuery.beginSearchTime = ''
  //       searchQuery.endSearchTime = ''
  //     }
  //   }
  // },
  methods: {
    setDateRange(rangeType) {
      let times = []
      switch (rangeType) {
        case '今日':
          times = dateRange.getCurrentDay()
          break
        case '本周':
          times = dateRange.getCurrentWeek()
          break
        case '本月':
          times = dateRange.getCurrentMonth()
          break
        case '上月':
          times = dateRange.getLastMonth()
          break
        case '全部':
          times = []
          break
      }
      if (times.length) {
        this.query.beginSearchTime = times[0].getTime()
        this.query.endSearchTime = times[1].getTime()
      } else {
        this.query.beginSearchTime = ''
        this.query.endSearchTime = ''
      }
      this.$emit('select', this.query)
    },
    search() {
      this.$emit('search', this.query)
    }
  }
}
</script>

<style scoped>

</style>